/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getCovereds;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Superclass for the commands that work on interaction fragment margins.
 * 
 * @author frankd@ifi.uio.no
 */
public abstract class AbstractMarginCommand extends Command {

    protected GraphNode fragment;

    public void setFragment(final GraphNode fragment) {
        this.fragment = fragment;
    }

    public GraphNode getFragment() {
        return fragment;
    }

    /**
     * Determines the left and right margins for the given fragment.
     * 
     * @return an array [leftmargin, rightmargin].
     */
    @SuppressWarnings("unchecked")
    protected int[] getMargins(GraphNode fragment) {
        int minx = Integer.MAX_VALUE;
        int maxx = Integer.MIN_VALUE;

        final Diagram diagram = LookForElement.getDiagram(fragment);
        final List<Lifeline> lifelineList = getFragmentCovereds(fragment);

        for (Lifeline coveredLifeline : lifelineList) {
            ArrayList<GraphNode> listGraph =
                    LookForElement.lookForGraphElementWODiagram(coveredLifeline, diagram,
                            new ArrayList<GraphNode>(1));
            final GraphNode lifelineView = listGraph.get(0);

            Rectangle bounds = new Rectangle(lifelineView.getPosition(), lifelineView.getSize());
            final int lifelinex = bounds.getCenter().x;
            if (lifelinex < minx)
                minx = lifelinex;
            if (lifelinex > maxx)
                maxx = lifelinex;
        }

        Rectangle bounds = new Rectangle(fragment.getPosition(), fragment.getSize());
        return new int[] {
                bounds.x - minx, bounds.x + bounds.width - maxx
        };
    }

    private List<Lifeline> getFragmentCovereds(GraphNode fragment) {
        if (ModelUtil.getSemantic(fragment) instanceof ExecutionSpecification) {
            ExecutionSpecification eo = ModelUtil.<ExecutionSpecification> getSemantic(fragment);
            if (eo.getStart() != null)
                return getCovereds(eo.getStart());
            else
                return Collections.emptyList();
        }
        return getCovereds(ModelUtil.<InteractionFragment> getSemantic(fragment));

    }

    @Override
    public void dispose() {
        fragment = null;
    }
}
